package com.lin.java.tool.general.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.java.tool.common.model.DataPage;
import com.lin.java.tool.common.model.QueryPage;
import com.lin.java.tool.file.service.FileService;
import com.lin.java.tool.general.dao.mapper.SysFileMapper;
import com.lin.java.tool.general.dao.po.SysFileEntity;
import com.lin.java.tool.general.service.SysFileService;
import com.lin.java.tool.mybatis.utils.QueryPageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: linjiang
 * @date: 2025/4/4
 */
@Service
public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFileEntity> implements SysFileService {

    @Autowired
    private FileService fileService;

    @Override
    public DataPage listPage(QueryPage query) {
        return QueryPageUtil.lambdaPageQuery(this, query, wrapper -> {
            wrapper.orderByDesc(SysFileEntity::getCreateTime)
                    .like(StringUtils.isNoneBlank(query.getKeyword()), SysFileEntity::getOriginFileName, query.getKeyword());
        }, e -> e);
    }

    @Override
    public String upload(MultipartFile file) throws Exception {
        return fileService.uploadFile(file);
    }

    @Override
    public void download(String fileId, HttpServletResponse response) throws IOException {
        fileService.downloadFile(fileId, response);
    }

}
